package com.dmall.sort.algorithm;

/**
 * 选择排序算法
 * @author xiao1.wang
 * @date 2021-10-08 18:15:29
 */
@SuppressWarnings("all")
public class SelectionSort implements Sort {

    @Override
    public void sort(int[] arr) {
        selectionSort(arr, arr.length);
    }

    private void selectionSort(int[] arr, int n) {
        if (n <= 1) {
            return;
        }
        for (int i = 0; i < n - 1; i++) {
            int minIndex = i;
            for (int j = i + 1; j < n; j++) {
                if (arr[minIndex] > arr[j]) {
                    minIndex = j;
                }
            }
            if (minIndex != i) {
                swap(arr, minIndex, i);
            }
        }
    }

    @Override
    public String getName() {
        return "选择排序";
    }

    public static void main(String[] args) {
        int[] arr = {6, 9, 4, 7, 2, 8, 3, 1, 10, 5};
        Sort.printArr(arr);
        Sort sort = new SelectionSort();
        sort.sort(arr);
        Sort.printArr(arr);
    }
}
